package com.wangp.mywrite.coding;

import java.util.Arrays;

/**
 * @author tooStronger
 * @date 2022/10/8
 */
public class AdvantageCount {

    public int[] advantageCount(int[] nums1, int[] nums2) {
        int n = nums1.length;
        Integer[] idx1 = new Integer[n];
        Integer[] idx2 = new Integer[n];

        for (int i = 0; i < n; i++) {
            idx1[i] = i;
            idx2[i] = i;
        }
        Arrays.sort(idx1, (i, j) -> nums1[i] - nums1[j]);
        Arrays.sort(idx2, (i, j) -> nums2[i] - nums2[j]);

        int[] ans = new int[n];
        int left = 0;
        int right = n-1;
        for (int i = 0; i < n; i++) {
            if(nums1[idx1[i]] > nums2[idx2[left]]){
                ans[idx2[left]] = nums1[idx1[i]];
                left++;
            }else{
                ans[idx2[right]] = nums1[idx1[i]];
                right--;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] nums1 = new int[]{1, 2, 3, 4};
        int[] nums2 = new int[]{4, 1, 2, 3};
        int[] res = new AdvantageCount().advantageCount(nums1, nums2);
        System.out.println(Arrays.toString(res));

    }
}
